package com.enation.app.javashop.core.handler;

import com.enation.app.javashop.framework.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;

/**
 * 熔断fallback基类，提供异常处理:
 * 如果是 ServiceException业务异常则抛出不进入fallback
 * 否则进入fallback
 * @author kingapex
 * @version 1.0
 * @data 2022/5/27 23:10
 **/
public abstract class BaseFallbackFactory<T> implements FallbackFactory {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public T create(Throwable cause) {

       //如果是 ServiceException业务异常则抛出不进入fallback
        if (cause instanceof ServiceException) {
            ServiceException se =(ServiceException) cause;
            throw se;
        }
        logger.error("feign调用系统异常:", cause);
        return fallback();
    }

    protected abstract T fallback();

}
